package com.imooc.Thread.DaemonDemo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

/**
 * Created by Administrator on 2017/7/18.
 */
class DaemonThread implements Runnable {
    @Override
    public void run() {
        System.out.println("程序进入到守护线程：" + Thread.currentThread().getName());
        try {
            writeToFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("程序退出到守护线程：" + Thread.currentThread().getName());
    }
    private void writeToFile() throws Exception{
        File fileName = new File("c:" + File.separator + "daemon.txt");
        OutputStream os = new FileOutputStream(fileName, true);
        Integer count = 0;
        while(count < 999) {
            os.write(("\r\nworld:" + count).getBytes());
            System.out.println("the daemonThread: " + count++ + ":" + Thread.currentThread().getName());

            Thread.sleep(500);
        }
    }
}
public class DaemonDemo {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("程序进入了主线程：" + Thread.currentThread().getName());
        DaemonThread daemonThread = new DaemonThread();
        Thread thread = new Thread(daemonThread);
//        thread.setDaemon(true);
        thread.start();
//        for (int i = 0; i < 10; i++) {
//            Thread.sleep(500);
//            thread.
//        }

        Scanner sc = new Scanner(System.in);
        sc.next();

        System.out.println("程序退出了主线程：" + Thread.currentThread().getName());
    }
}

